<?php
session_start();

// 验证用户是否登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php');
    exit();
}
require_once 'track_online.php';
// 获取用户信息
$username = $_GET['user'] ?? null;
if (!$username || !is_dir(__DIR__ . "/user/$username")) {
    die("用户不存在！");
}

$userDir = __DIR__ . "/user/$username";
$infoFile = "$userDir/info.json";
$userInfo = file_exists($infoFile) ? json_decode(file_get_contents($infoFile), true) : null;

if (!$userInfo) {
    die("用户信息不可用！");
}

// 加载中心化称号文件
$titlesFile = __DIR__ . '/ch.json';
$allTitles = file_exists($titlesFile) ? json_decode(file_get_contents($titlesFile), true) : [];
$userTitles = $allTitles[$username] ?? [];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($username) ?> 的资料 - JIILCHAT</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }
        
        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
        }
        
        /* 顶部导航栏 */
        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background: #ffffff;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            z-index: 100;
            border-bottom: 1px solid #e0e4e9;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #000; /* 黑色箭头 */
            font-size: 20px;
            margin-right: 15px;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: #e4e6eb;
        }
        
        .navbar-title {
            font-size: 18px;
            font-weight: 600;
            flex-grow: 1;
            color: #333;
        }
        
        /* 主内容区 */
        .content {
            margin-top: 70px;
            padding: 20px;
        }
        
        /* 用户信息卡片 */
        .profile-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        
        .profile-header {
            display: flex;
            align-items: center;
            margin-bottom: 25px;
        }
        
        .avatar-container {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            overflow: hidden;
            margin-right: 20px;
            background: #e0e4e9;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .avatar {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .profile-info {
            flex-grow: 1;
        }
        
        .username-container {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 10px;
        }
        
        .username {
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }
        
        /* 称号标签样式 */
        .title-tag {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
            color: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .user-meta {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 15px;
        }
        
        .meta-item {
            display: flex;
            align-items: center;
            font-size: 15px;
            color: #7f8c8d;
        }
        
        .meta-item i {
            margin-right: 8px;
            font-size: 16px;
        }
        
        .bio {
            font-size: 16px;
            color: #555;
            line-height: 1.6;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
        }
        
        /* 详细信息卡片 */
        .details-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        
        .card-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 1px solid #f0f2f5;
            color: #333;
        }
        
        .detail-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
        }
        
        .detail-item {
            margin-bottom: 15px;
        }
        
        .detail-label {
            font-weight: 500;
            color: #7f8c8d;
            margin-bottom: 5px;
            font-size: 14px;
        }
        
        .detail-value {
            color: #495057;
            font-size: 16px;
        }
        
        /* 称号展示区 */
        .titles-container {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        
        .titles-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
        }
        
        .title-card {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            transition: all 0.3s ease;
        }
        
        .title-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .title-name {
            font-size: 16px;
            font-weight: 600;
            color: #333;
            margin-bottom: 10px;
        }
        
        .title-color {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            margin: 0 auto;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        /* 添加好友按钮 */
        .add-friend-btn {
            display: block;
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #333, #000); /* 黑色按钮 */
            color: white;
            text-align: center;
            border-radius: 12px;
            font-size: 17px;
            font-weight: 600;
            text-decoration: none;
            box-shadow: 0 4px 15px rgba(0,0,0,0.3);
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
        }
        
        .add-friend-btn:hover {
            background: linear-gradient(135deg, #222, #000);
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.4);
        }
        
        .add-friend-btn i {
            margin-right: 10px;
            font-size: 20px;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .profile-header {
                flex-direction: column;
                text-align: center;
            }
            
            .avatar-container {
                margin-right: 0;
                margin-bottom: 20px;
            }
            
            .username-container {
                justify-content: center;
            }
            
            .detail-grid, .titles-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="navbar">
        <button class="back-btn" onclick="history.back()">
            <i class="fas fa-arrow-left"></i>
        </button>
        <div class="navbar-title"><?= htmlspecialchars($username) ?> 的资料</div>
    </div>
    
    <div class="content">
        <!-- 用户信息卡片 -->
        <div class="profile-card">
            <div class="profile-header">
                <div class="avatar-container">
                    <?php if (!empty($userInfo['avatar'])): ?>
                        <img src="<?= htmlspecialchars($userInfo['avatar']) ?>" alt="头像" class="avatar">
                    <?php else: ?>
                        <i class="fas fa-user fa-3x" style="color: #7f8c8d;"></i>
                    <?php endif; ?>
                </div>
                <div class="profile-info">
                    <div class="username-container">
                        <div class="username"><?= htmlspecialchars($username) ?></div>
                        <!-- 显示用户称号标签 -->
                        <?php if (!empty($userTitles)): ?>
                            <?php foreach ($userTitles as $title): ?>
                                <span class="title-tag" style="background-color: <?= $title['color'] ?>;">
                                    <?= htmlspecialchars($title['name']) ?>
                                </span>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                    
                    <div class="user-meta">
                        <?php if (isset($userInfo['gender'])): ?>
                            <div class="meta-item">
                                <i class="fas <?= $userInfo['gender'] === '男' ? 'fa-mars' : ($userInfo['gender'] === '女' ? 'fa-venus' : 'fa-genderless') ?>"></i>
                                <?= htmlspecialchars($userInfo['gender']) ?>
                            </div>
                        <?php endif; ?>
                        
                        <?php if (isset($userInfo['zodiac_sign'])): ?>
                            <div class="meta-item">
                                <i class="fas fa-star"></i>
                                <?= htmlspecialchars($userInfo['zodiac_sign']) ?>
                            </div>
                        <?php endif; ?>
                        
                        <?php if (isset($userInfo['birthday'])): ?>
                            <div class="meta-item">
                                <i class="fas fa-birthday-cake"></i>
                                <?= htmlspecialchars($userInfo['birthday']) ?>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <?php if (isset($userInfo['bio'])): ?>
                        <div class="bio"><?= htmlspecialchars($userInfo['bio']) ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- 详细信息卡片 -->
        <div class="details-card">
            <div class="card-title">详细信息</div>
            <div class="detail-grid">
                <?php if (isset($userInfo['birthplace'])): ?>
                    <div class="detail-item">
                        <div class="detail-label">出生地</div>
                        <div class="detail-value"><?= htmlspecialchars($userInfo['birthplace']) ?></div>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($userInfo['school'])): ?>
                    <div class="detail-item">
                        <div class="detail-label">学校</div>
                        <div class="detail-value"><?= htmlspecialchars($userInfo['school']) ?></div>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($userInfo['current_residence'])): ?>
                    <div class="detail-item">
                        <div class="detail-label">现居地</div>
                        <div class="detail-value"><?= htmlspecialchars($userInfo['current_residence']) ?></div>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($userInfo['zodiac_sign'])): ?>
                    <div class="detail-item">
                        <div class="detail-label">星座</div>
                        <div class="detail-value"><?= htmlspecialchars($userInfo['zodiac_sign']) ?></div>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($userInfo['birthday'])): ?>
                    <div class="detail-item">
                        <div class="detail-label">生日</div>
                        <div class="detail-value"><?= htmlspecialchars($userInfo['birthday']) ?></div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <!-- 称号展示区 -->
        
        <!-- 添加好友按钮 -->
        <a href="add_user.php" class="add-friend-btn">
            <i class="fas fa-user-plus"></i> 添加好友
        </a>
    </div>
</body>
</html>